package org.jeecg.modules.flow.record.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.flow.record.entity.WmsDocumentTypeStatusReminder;
import org.jeecg.modules.flow.record.mapper.WmsDocumentTypeStatusReminderMapper;
import org.jeecg.modules.flow.record.service.IWmsDocumentTypeStatusReminderService;
import org.jeecg.modules.flow.record.service.IWmsDocumentTypeStatusUserService;
import org.jeecg.modules.flow.sys.enumx.FlowRecordStatusEnum;
import org.jeecg.modules.wms.sys.entity.SysSmsTemplate;
import org.jeecg.modules.wms.sys.mapper.SysSmsTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 流程配置警示提示配置
 * @Author: jeecg-boot
 * @Date: 2023-11-30
 * @Version: V1.0
 */
@Service
public class WmsDocumentTypeStatusReminderServiceImpl extends ServiceImpl<WmsDocumentTypeStatusReminderMapper, WmsDocumentTypeStatusReminder> implements IWmsDocumentTypeStatusReminderService {

    @Autowired
    private IWmsDocumentTypeStatusUserService wmsDocumentTypeStatusUserService;
    @Autowired
    private SysSmsTemplateMapper sysSmsTemplateMapper;

    @Override
    public List<WmsDocumentTypeStatusReminder> listByStatusId(String statusId) {

        List<WmsDocumentTypeStatusReminder> list = this.list(
                new LambdaQueryWrapper<WmsDocumentTypeStatusReminder>().eq(WmsDocumentTypeStatusReminder::getDocumentStatusId, statusId));
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
            List<FlowRecordStatusEnum> flowRecordStatusEnums = FlowRecordStatusEnum.getAllObjects();
            for (FlowRecordStatusEnum statusEnum : flowRecordStatusEnums) {
                WmsDocumentTypeStatusReminder reminder = new WmsDocumentTypeStatusReminder();
                reminder.setStatus(String.valueOf(statusEnum.getCode()));
                reminder.setDocumentStatusId(statusId);
                list.add(reminder);
            }
            this.saveBatch(list);
        }
        List<String> templateCodes = list.stream().map(t -> t.getReminderTemplateCode()).filter(t -> StringUtils.isNotBlank(t)).collect(Collectors.toList());
        Map<String, String> sysSmsTemplatesMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(templateCodes)) {
            List<SysSmsTemplate> sysSmsTemplates = sysSmsTemplateMapper.selectList(new LambdaQueryWrapper<SysSmsTemplate>().in(SysSmsTemplate::getTemplateCode, templateCodes));
            sysSmsTemplatesMap = sysSmsTemplates.stream().collect(Collectors.toMap(t -> t.getTemplateCode(), a -> a.getTemplateName()));
        }
        list = list.stream().sorted(Comparator.comparing(WmsDocumentTypeStatusReminder::getStatus))
                .collect(Collectors.toList());
        Map<String, String> finalSysSmsTemplatesMap = sysSmsTemplatesMap;
        list.forEach(t -> {
            t.setReminderObject(wmsDocumentTypeStatusUserService.getUserName(t.getId()));
            t.setStatusName(FlowRecordStatusEnum.getName(Integer.valueOf(t.getStatus())));
            if (ObjectUtil.isNotEmpty(finalSysSmsTemplatesMap.get(t.getReminderTemplateCode()))) {
                t.setReminderTemplateName(finalSysSmsTemplatesMap.get(t.getReminderTemplateCode()));
            }
        });
        return list;
    }

    @Override
    public void updateList(List<WmsDocumentTypeStatusReminder> wmsDocumentTypeStatusReminders) {
        this.updateBatchById(wmsDocumentTypeStatusReminders);
    }
}
